<template>
    <div>
        <!-- 导航部分开始 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>组织架构</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!--  搜索栏开始 -->
        <el-form style="margin-top: 15px;" :inline="true" :model="queryUser" class="demo-form-inline">
            <el-form-item label="用户名">
                <el-input v-model="queryUser.username" placeholder="用户名" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="loadUsers">搜索</el-button>
            </el-form-item>
        </el-form>
        <!--  搜索栏结束 -->
        <!-- 工具栏开始 -->
        <div class="flex">
            <el-button title="新增用户" @click="this.addUserDialogVisible = true" type="primary"><el-icon>
                    <Plus />
                </el-icon></el-button>
            <el-button @click="handlerEditDicts" title="编辑选中用户" type="primary"><el-icon>
                    <EditPen />
                </el-icon></el-button>
            <el-button @click="handlerDeleteDicts" title="删除选中用户" type="primary"><el-icon>
                    <Delete />
                </el-icon></el-button>
            <el-button @click="exportDict" title="导出数据" type="primary"><el-icon>
                    <Download />
                </el-icon></el-button>
            <el-upload style="display: inline; vertical-align: middle; margin: 0px 10px;" :action="importUsersUrl"
                name="uploadFile" :show-file-list="false" :on-success="importSuccess">
                <el-button title="导入数据" type="primary"><el-icon>
                        <Upload />
                    </el-icon></el-button>
            </el-upload>
        </div>
        <!-- 工具栏结束 -->
        <!-- 表格 开始 -->
        <el-table ref="userTable" :data="users" max-height="800px" stripe="true" border="true"
            style="width: 90%;margin-top: 10px;">
            <el-table-column align="center" type="selection" width="55" />
            <el-table-column align="center" label="#" property="userId" />
            <el-table-column align="center" label="用户名" property="username" />
            <el-table-column align="center" label="用户昵称" property="nickname" />
            <el-table-column align="center" label="所属部门" property="dept.deptName" />
            <el-table-column align="center" label="状态" property="dictStatus">
                <template #default="scope">
                    <!-- scope.row 就是整行数据-->
                    <el-tag v-if="scope.row.userStatus == 1" class="ml-2" type="success">正常</el-tag>
                    <el-tag v-else class="ml-2" type="danger">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column align="center" label="创建时间" :formatter="timeFormatter" property="createTimeMillis" />
            <el-table-column align="center" label="最后修改时间" :formatter="timeFormatter" property="modifyTimeMillis" />
            <el-table-column width="150" align="center" label="操作">
                <template #default="scope">
                    <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-popconfirm confirm-button-text="是" cancel-button-text="点错了" @confirm="handleDelete(scope.row)"
                        title="您是否要删除这行数据">
                        <template #reference>
                            <el-button size="small" type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 表格 结束 -->
    </div>
    <!-- 分页栏开始 -->
    <el-pagination style="margin-top: 15px;" :page-size="usersLimit" :pager-count="usersMaxPage"
        @current-change="changePage" background layout="prev, pager, next" :total="usersTotal" />
    <!-- 分页栏结束 -->
    <!-- 添加用户弹窗窗体开始-->
    <el-dialog v-model="addUserDialogVisible" title="添加用户" width="600" align-center>
        <!-- 添加用户的表单 -->
        <el-form label-width="100px" :model="newUser" style="max-width: 460px">
            <el-form-item label="所属部门">
                <el-tree-select 
                placeholder="请选择部门"
                v-model="newUser.dept.deptId" 
                :data="depts" 
                :props="defaultProps"
                :render-after-expand="false"/>
            </el-form-item>
            <el-form-item label="用户名">
                <el-input v-model="newUser.username" />
            </el-form-item>
            <el-form-item label="用户昵称">
                <el-input v-model="newUser.nickname" />
            </el-form-item>
            <el-form-item label="用户电话">
                <el-input v-model="newUser.userPhone" />
            </el-form-item>
            <el-form-item label="登陆密码">
                <el-input v-model="newUser.userPass" type="password" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="cancelAddUser">取消</el-button>
                <el-button type="primary" @click="doAddUser">
                    提交
                </el-button>
            </span>
        </template>
    </el-dialog>
    <!-- 添加用户的弹窗窗体结束-->
</template>
<script>
import tools from '../../../util/tools.js';
import userRequest from "../../../aip/user-api.js";
import deptRequest from "../../../aip/dept-api.js";
export default {
    data() {
        return {
            defaultProps: {
                children: 'children',
                label: 'deptName',
                value: "deptId"
            },
            queryUser: {},
            addUserDialogVisible: false,
            importUsersUrl: "",
            users: [],
            usersLimit: 10,
            usersMaxPage: 1,
            usersPage: 1,
            usersTotal: 0,
            newUser: { dept: {} },
            depts: [],
        }
    }, methods: {
        handleNodeClick() {

        },
        cancelAddUser() {
            this.addUserDialogVisible = false;
            this.newUser = { dept: {} }
        },
        doAddUser() {
            userRequest.save(this.newUser,res=>{
                if(res.code == 200){
                    this.$message({
                        type:"success",
                        message:"添加成功"
                    });
                    this.cancelAddUser();
                    this.loadUsers();
                }else{
                    this.$message({
                        type:"error",
                        message:"添加失败"
                    });
                }
            })
        },
        loadDepts() {
            deptRequest.list(res => {
                this.depts = res.data;
            });
        },
        loadUsers() {
            userRequest.list(this.usersPage, this.usersLimit, this.queryUser, res => {
                this.users = res.data.list;
                this.usersTotal = res.data.total;
                this.usersMaxPage = res.data.pages;
            })
        },
        handlerEditDicts() { },
        handlerDeleteDicts() { },
        exportDict() { },
        importSuccess() { },
        /**
         * 时间毫秒数转换为对应的字符串
         * @param {*} time 
         */
        timeFormatter(row, column, time) {
            return tools.timeMillisToString(time);
        }, changePage(value) {
            this.usersPage = value;
            this.loadUsers();
        },
    }, mounted() {
        this.loadUsers();
        this.loadDepts();
    }
}
</script>